﻿using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WLWZGao6.APIHost.help;
using WLWZGao6.APItitlion.IService;
using WLWZGao6.Domain.Common;
using WLWZGao6.Domain.Model;
using MD5Hash;
using WLWZGao6.Domain.Dto;
using System.Collections.Generic;


namespace WLWZGao6.APIHost.Controllers
{
    /// <summary>
    /// 用户控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class UserController : ControllerBase
    {
        /// <summary>
        /// 用户服务接口
        /// </summary>
        public IUserService _userService;
        /// <summary>
        /// JWT服务接口
        /// </summary>
        public TokenHelper _tokenHelper;
        /// <summary>
        /// 接口实现
        /// </summary>
        /// <param name="userService"></param>
        /// <param name="tokenHelper"></param>
        public UserController(TokenHelper tokenHelper,IUserService user)
        {
            _userService = user;
            _tokenHelper = tokenHelper;
        }
        [HttpPost]
        public IActionResult UserLogin(UserLoginDto user)
        {
            var res = new ResponseModel();

            var userInfo = _userService.CheckuserAndpwd(user.UserName, user.UserPass);
            if(userInfo !=null)
            {
                Dictionary<string, string> keyValuePairs = new Dictionary<string, string>
                {
                    { "UserName",user.UserName},
                    {"UserPass",user.UserPass }
                };
                res.Code = 200;
                res.Msg = "登录成功!";

                res.TokenInfo = _tokenHelper.CreateToken(keyValuePairs);
                return Ok(res);
            }else
            {
                res.Code = 401;
                res.Msg = "用户名或密码不正确";
                return Unauthorized(res);//401错误
            }
            
        }

        /// <summary>
        /// 用户登录
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        public EData Login(UserLoginDto user)
        {
            return _userService.Login(user);
        }
        /// <summary>
        /// 用户添加
        /// </summary>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        public EData UserAdd(User user)
        {
            return _userService.UserAdd(user);
        }
        /// <summary>
        /// 用户删除
        /// </summary>
        /// <param name="Ids"></param>
        /// <returns></returns>
        [HttpPost]
        public EData UserDel(int Ids)
        {
            return _userService.UserDel(Ids);
        }
        /// <summary>
        /// 用户显示
        /// </summary>
        /// <param name="Name"></param>
        /// <returns></returns>
        [HttpGet]
        public EData UserShow(string Name)
        {
            return _userService.UserShow(Name);
        }
        /// <summary>
        /// 用户修改
        /// </summary>
        /// <param name="Ids"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        [HttpPost]
        public EData UserUpd(int Ids, User user)
        {
            return _userService.UserUpd(Ids, user);
        }
        
    }
}
